package luceneqe.modelsIR;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;

public class SimilarityBM25 extends Similarity {
	
	enum IDFType {BIM, VSM};
	public static final IDFType idfType = IDFType.VSM;
	
	@Override
	public float coord(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public float idf(int n, int N) {		
		if (idfType.equals(IDFType.VSM))
			return (float) Math.log(N/(n+.5));
		else if (idfType.equals(IDFType.BIM))		
			return (float) Math.log( (N-n+.5)/(n+.5) );
		
		return -1;
	}

	public float idf_fromIndex(Term term, Searcher searcher) throws IOException {		
		return idf(searcher.docFreq(term), searcher.maxDoc());
	}

	@Override
	public float lengthNorm(String arg0, int arg1) {
		return (float)arg1;
	}

	@Override
	public float queryNorm(float arg0) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public float sloppyFreq(int arg0) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public float tf(float tf) {
		// we do not need square root here, BM25 score squashes tf.
		return tf;
	}

}
